#include "widget.h"
#include "ui_widget.h"

#include <QMimeData>
#include <QFileInfo>

void Widget::dragEnterEvent(QDragEnterEvent *event)
{
    ui->plainTextEdit->clear();
    ui->plainTextEdit->appendPlainText("dragEnterEvent事件 mimeData()->formats()");
    for(int i = 0; i < event->mimeData()->formats().size(); ++ i)
        ui->plainTextEdit->appendPlainText(event->mimeData()->formats().at(i));

    ui->plainTextEdit->appendPlainText("\n dragEnterEvent事件 mimeData()->urls()");
    for(int i = 0; i < event->mimeData()->urls().size(); ++ i)
    {
        QUrl url = event->mimeData()->urls().at(i);
        ui->plainTextEdit->appendPlainText(url.path());
    }

    if(event->mimeData()->hasUrls())
    {
        QString fileName = event->mimeData()->urls().at(0).fileName();
        QFileInfo fileInfo(fileName);
        QString ext = fileInfo.suffix().toUpper();
        if(ext == "JPG")
            event->acceptProposedAction();
        else
            event->ignore();
    }
    else
        event->ignore();
}

void Widget::resizeEvent(QResizeEvent *event)
{
    QSize sz = ui->plainTextEdit->size();
    ui->plainTextEdit->resize(this->width() - 10, sz.height());
    ui->labPic->resize(this->width() -10, this->height() - sz.height() - 20);
    event->accept();
}

void Widget::dropEvent(QDropEvent *event)
{
    QString fileName = event->mimeData()->urls().at(0).path();
    fileName = fileName.right(fileName.length() - 1);
    QPixmap pixmap(fileName);
    ui->labPic->setPixmap(pixmap);
    event->accept();
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->labPic->setScaledContents(true);

    this->setAcceptDrops(true);
    ui->plainTextEdit->setAcceptDrops(false);
    ui->labPic->setAcceptDrops(false);
}

Widget::~Widget()
{
    delete ui;
}
